package meta

import (
	"fmt"
	"strconv"
	"time"

	"github.com/micro-plat/lib4go/types"
)

func parseMap(rows types.XMaps) map[string]interface{} {
	m := make(map[string]interface{})
	for _, row := range rows {
		m[row.GetString("key")] = row["value"]
	}
	return m
}

// 解析配置项
func parseConf(row types.IXMap) (conf types.IXMap, err error) {
	conf = types.NewXMap()
	value, err := parseValue(row.GetString("value"), VType(row.GetInt("vtype")))
	if err != nil {
		return nil, err
	}
	conf.SetValue("key", row.GetString("key"))
	conf.SetValue("value", value)
	return conf, nil
}

// 为了处理JSON等复杂类型（可选）
// 假设value字段可能存储JSON字符串，我们需要一个更通用的解析方法
func parseValue(value string, vtype VType) (interface{}, error) {
	switch vtype {
	case VTypeString:
		return value, nil
	case VTypeNumber:
		return strconv.ParseInt(value, 10, 64)
	case VTypeFloat:
		return strconv.ParseFloat(value, 64)
	case VTypeBool:
		return strconv.ParseBool(value)
	case VTypeDate:
		return time.Parse(time.RFC3339, value)
	default:
		return nil, fmt.Errorf("unknown vtype: %d", int(vtype))
	}
}
